package com.boot.security.server.controller.mffl.MffCashwithdrawalapplication;

import com.boot.security.server.dao.mffl.MffCashwithdrawalapplicationDao;
import com.boot.security.server.model.SysUser;
import com.boot.security.server.model.mffl.MffCashwithdrawalapplication;
import com.boot.security.server.model.mffl.MfflBankcard;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.service.impl.mffl.MffCashwithdrawalapplication.MffCashwithdrawalapplicationService;
import com.boot.security.server.service.impl.mffl.Mfflbankcard.BankCardService;
import com.boot.security.server.utils.UserUtil;
import com.boot.security.server.utils.mffl.ReturnBean;
import io.swagger.annotations.*;
import org.apache.commons.collections4.Put;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import com.boot.security.server.service.impl.mffl.Mfflbankcard.BankCardService;

@Api(tags = "提现")
@RestController
@RequestMapping("/client/mffCashwithdrawalapplications")
public class Client_MffCashwithdrawalapplicationController {

    @Autowired
    private MffCashwithdrawalapplicationDao mffCashwithdrawalapplicationDao;

    //银行卡
    @Autowired
    private BankCardService BankCardService;

    //提现业务
    @Autowired
    private MffCashwithdrawalapplicationService MffCashwithdrawalapplicationService;

    @GetMapping(value = "showbankcard")
    @ApiOperation(value="获取银行卡信息")
    public ReturnBean<MfflBankcard> showBankCard(){
        return BankCardService.showBankCard();
    }

    @GetMapping(value = "getusermoneyall")
    @ApiOperation(value="获取账户全部金额")
    public ReturnBean getUserMoneyAll(){
        return MffCashwithdrawalapplicationService.getUserMoneyAll();
    }


    @PostMapping(value = "applycashwithdrawal")
    @ApiOperation(value = "申请提现")
    public ReturnBean applyCashWithdrawal(@ApiParam(name = "money",value = "提现金额",required = true) @RequestParam String money,
                                          @ApiParam(name = "bankcardnumber",value = "银行卡号",required = true) String bankcardnumber) throws Exception{
        if(StringUtils.isEmpty(money)){
            return new ReturnBean("201", "金额不可为空");
        }if(StringUtils.isEmpty(bankcardnumber)){
            return new ReturnBean("201", "银行卡号不可为空");
        }if(Double.parseDouble(money) == 0){
            return new ReturnBean("201", "提现金额不允许为0");
        }
        return MffCashwithdrawalapplicationService.applyCashWithdrawal(Double.parseDouble(money),bankcardnumber);
    }

  /*@PostMapping(value = "lookwithdrawalrecord")
    @ApiOperation(value = "查询提现记录")
    public ReturnBean lookWithdrawalRecord() {
        return MffCashwithdrawalapplicationService.applyCashWithdrawal(Double.parseDouble(money),bankcardnumber);
    }*/

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取查询提现记录")
    public ReturnBean<MffCashwithdrawalapplication> get(@PathVariable Long id) {
        if(id == null){
            return new ReturnBean("201", "id不允许为空");
        }
        return MffCashwithdrawalapplicationService.lookWithdrawalRecord(id);
    }

    @PostMapping(value = "cancel")
    @ApiOperation(value = "根据id取消提现申请")
    public ReturnBean cancel(@ApiParam(name = "id",value="id",required = true)@RequestParam Long id) {
        if(id == null){
            return new ReturnBean("201", "id不允许为空");
        }
        return MffCashwithdrawalapplicationService.cancel(id);
    }

  /*  @PutMapping
    @ApiOperation(value = "修改")
    public MffCashwithdrawalapplication update(@RequestBody MffCashwithdrawalapplication mffCashwithdrawalapplication) {
        mffCashwithdrawalapplicationDao.update(mffCashwithdrawalapplication);

        return mffCashwithdrawalapplication;
    }*/

    @GetMapping
    @ApiOperation(value = "提现记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "offset", value = "开始条数", dataType = "int"),
            @ApiImplicitParam(required = true, name = "limit", value = "每页条数", dataType = "int")})
    public ReturnBean<MffCashwithdrawalapplication> list(PageTableRequest request) {
        SysUser sysUser = UserUtil.getLoginUser();
        request.setParams(new HashMap<String,Object>(){{put("userid",sysUser.getId());put("orderBy","ORDER BY createTime DESC");}});

        PageTableResponse handle = new PageTableHandler(new CountHandler() {
            @Override
            public int count(PageTableRequest request) {
                return mffCashwithdrawalapplicationDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<MffCashwithdrawalapplication> list(PageTableRequest request) {
                return mffCashwithdrawalapplicationDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
        ReturnBean ReturnBean = new ReturnBean();
        ReturnBean.setDatas(handle);
        return ReturnBean;
    }

 /*   @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        mffCashwithdrawalapplicationDao.delete(id);
    }*/
}
